ParamArray Keyword

Used in a Sub or Function statement to indicate that an arbitrary number of parameters of the specified data type can be passed.

ParamArray [parameterName As DataType]

PartDescription
parameterName Name of parameter for which an indefinite number of arguments will be passed.
DataType Data type of the parameter. It can be any valid data type.


Notes

The ParamArray keyword enables you to pass an indefinite number of values of a specific data type without formally using an array. A call to a method or function that has been declared using ParamArray uses a comma-delimited list of values rather than an array.

Normally you would pass a parameter to a method defined using ParamArray but the parameter is optional.


Example

The following function adds a list of numbers that are passed via ParamArray.

Function AddNumbers(ParamArray Nums as Integer) As Integer
  Dim i,Total as Integer
 For Each i in Nums
  total=total+i
 Next
  Return Total

You can call this function with the following code:

Dim n as Integer
n=AddNumbers(5,10,20)

Any number of integers can be passed to AddNumbers.

This approach is equivalent to the use of an array. Consider the following function:

Function AddArray(Nums() as Integer) As Integer
Dim i, total as Integer
For Each i in Nums
  total=total+i
 Next
Return total

You can initialize the array and call the function in the following way:

Dim myArray(-1) as Integer //declare array without specifying size
Dim n as Integer
myArray= Array(5,10,20) //create 3 elements and assign values
n=AddArray(myArray)

See Also

Array function; Dim, Function, Sub statements.